<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite pt20 pb10 radius10 pl10 pr20 justify-between">
      <div>
        <el-select v-model="city" placeholder="選擇城市" size="large" style="width: 240px" class="mr10 mb10">
          <el-option v-for="item in cityOptions" :key="item.id" :label="item.city" :value="item.city" />
        </el-select>
        <el-input v-model="uid" style="width: 280px" placeholder="搜尋用戶訊息..." :prefix-icon="Search" class="search mr10 mb10" />
        <el-input v-model="nickname" style="width: 280px" placeholder="搜尋昵稱..." :prefix-icon="Search" class="search mr10 mb10" />
        <el-select v-model="online" placeholder="在綫狀態" size="large" style="width: 240px" class="mb10">
          <el-option v-for="item in onlineOptions" :key="item.label" :label="item.label" :value="item.value" />
        </el-select>


      </div>
      <div class="noshrink mb10">
        <el-button class="btn2" @click="search()">查詢</el-button>
        <el-button class="btn1" @click="reset()">重置</el-button>
      </div>
    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="playerTable" style="width: 100%" cell-class-name="tbcell" header-cell-class-name="hdcell">
          <el-table-column prop="" label="用戶訊息" width="200">
            <template #default="scope">
              <span @click="showUser(scope.row.uid)">{{ scope.row.phone }}(ID:{{ scope.row.uid }})</span>
            </template>
          </el-table-column>
          <el-table-column prop="nickname" label="昵稱" width="150" />

          <el-table-column prop="sex" label="性別" width="80">
            <template #default="scope">
              <span>{{ scope.row.sex == 1 ? '男' : '女' }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="height" label="身高" width="80" />
          <el-table-column prop="weight" label="體重(kg)" width="80" />
          <el-table-column prop="city" label="城市" width="100" />
          <el-table-column prop="introduction" label="簡介" width="200" />
          <el-table-column prop="voice" label="語音" width="350">
            <template #default="scope">
              <audio controls :src="scope.row.voice"></audio>
            </template>
          </el-table-column>

          <el-table-column prop="cover_photo" label="封面圖片" width="100">
            <template #default="scope">
              <el-avatar shape="square" :size="50" :fit="'cover'" :src="scope.row.cover_photo" />
            </template>
          </el-table-column>
          <el-table-column prop="my_album" label="個人相冊" width="100">
            <template #default="scope">
              <el-image style="width: 50px; height: 50px" :src="scope.row.my_album[0]" :zoom-rate="1.2" :max-scale="7" :min-scale="0.2" :preview-src-list="scope.row.my_album" :initial-index="0" fit="cover" />
            </template>
          </el-table-column>
          <el-table-column prop="online_text" label="在綫狀態" width="100" />
          <el-table-column prop="is_recommend_text" label="首頁推薦" width="100" />
          <el-table-column prop="is_freeze" label="是否凍結" width="100">
            <template #default="scope">
              <span>{{ scope.row.is_freeze == 0 ? '否' : '是' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="縂收入" width="150" sortable>
            <template #default="scope">
              <div class="align-center" @click="dialogShow(scope.row)">
                <span class="mr5">{{ scope.row.total }}</span>
                <el-icon>
                  <View />
                </el-icon>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="created_at" label="創建時間" width="200" />
          <el-table-column prop="updated_at" label="更新時間" width="200" />
          <el-table-column prop="address" label="操作" width="300" fixed="right">
            <template #default="scope">
              <div class="align-center">
                <div class=" pointer" @click="checkEvaluate(scope.row)">評價</div>
                <span class="plr10">|</span>
                <div class=" pointer" @click="checkOrder(scope.row)">訂單</div>
                <span class="plr10">|</span>
                <div class=" pointer" @click="checkPrice(scope.row)">服務價格</div>
                <span class="plr10">|</span>
                <div class=" pointer" @click="edit(scope.row)">編輯</div>
              </div>
            </template>
          </el-table-column>

        </el-table>
      </div>
      <div class="plr20 mlauto mt20">
        <el-pagination v-model:current-page="currentPage1" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total1" @size-change="handleSizeChange" @current-change="handleCurrentChange1" />
      </div>

    </div>
    <el-dialog v-model="dialogTableVisible" title="总收入" width="1000">
      <div class="plr20 justify-between ptb20">
        <div>
          <el-date-picker v-model="start" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10">-</span>
          <el-date-picker v-model="end" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
        </div>
        <div class="noshrink">
          <el-button class="btn2" @click="search2()">查詢</el-button>
          <el-button class="btn1" @click="reset2()">重置</el-button>
        </div>

      </div>
      <el-table :data="incomeTable" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
        <el-table-column prop="id" label="序號" width="80" />

        <el-table-column prop="fromid" label="用戶訊息" />
        <el-table-column prop="created_at" label="時間" />
        <el-table-column prop="money" label="收入金額">
        </el-table-column>
        <el-table-column prop="money_type_text" label="收入類型" />

      </el-table>
      <div class=" plr20 justify-between mt20">
        <div class="clr3c4 bold noshrink">總共{{ total2 }}條</div>
        <div>
          <div>小計(元): <span class="ft46 clr3c4 ml10">{{ totalMoney }}</span> </div>
        </div>
      </div>
      <div class="flex-col w100p">
        <div class="plr20 mt20 mlauto">
          <el-pagination v-model:current-page="currentPage2" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total2" @size-change="handleSizeChange" @current-change="handleCurrentChange2" />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import { playerList, receiveList, cityList } from '@/api/player.js'
import eventBus from '@/utils/eventBus'
import { getPathIndex } from '@/utils/utils'
import { inject } from 'vue';
const showUser = inject('showUser');

onMounted(() => {
  getPlayerList()
  getCityList()
})
// 城市列表
const getCityList = () => {
  cityList().then((res) => {
    console.log('城市列表', res)
    cityOptions.value = res.data
  })
}
// 陪玩列表
const getPlayerList = () => {
  playerList({
    page: currentPage1.value,
    uid: uid.value,
    city: city.value,
    nickname: nickname.value,
    online: online.value
  }).then((res) => {
    console.log('等级列表', res)
    playerTable.value = res.data.data
    total1.value = res.data.total
  })
}
// 总收入列表
const getReceiveList = () => {
  receiveList({
    uid: currentId.value,
    start: start.value,
    end: end.value,
    page: currentPage2.value
  }).then((res) => {
    console.log('总收入列表', res)
    incomeTable.value = res.data.data
    total2.value = res.data.total
    totalMoney.value = res.total
  })
}
// 重置
const reset = () => {
  uid.value = ''
  city.value = ''
  nickname.value = ''
  online.value = ''
  getPlayerList()

}
// 搜尋
const search = () => {
  getPlayerList()
}
// 重置
const reset2 = () => {
  start.value = ''
  end.value = ''
  getReceiveList()

}
// 搜尋
const search2 = () => {
  getReceiveList()
}
const start = ref('')
const end = ref('')
const currentId = ref('')
const input2 = ref('')
const city = ref('')
const uid = ref('')
const nickname = ref('')
const online = ref('')
const dialogTableVisible = ref()
const totalMoney = ref(0)
const dialogShow = (row) => {
  dialogTableVisible.value = true
  currentId.value = row.uid
  getReceiveList()
}
const cityOptions = ref([])
const onlineOptions = [
  {
    label: '在线',
    value: '1'
  },
  {
    label: '忙碌',
    value: '2'
  },
  {
    label: '离线',
    value: '3'
  }
]
const playerTable = ref([]) //陪玩列表数据
const incomeTable = ref([]) //总收入列表数据


const currentPage1 = ref(1) //陪玩列表分页
const total1 = ref(0)
const currentPage2 = ref(1) //总收入列表分页
const total2 = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange1 = (val) => {
  console.log(`current page: ${val}`)
  getPlayerList()
}
const handleCurrentChange2 = (val) => {
  console.log(`current page: ${val}`)
  getReceiveList()
}
const router = useRouter()
const toAdd = () => {
  console.log(router)
  router.push({ path: 'addNotice' })
}
const edit = (row) => {
  router.push({ path: 'editPlayerList', query: row })
}
const checkPrice = (row) => {
  eventBus.emit('activeChild', 3)

  router.push({ path: 'servicePrice', query: { uid: row.uid } })
}
const checkOrder = (row) => {
  let parentPath = getPathIndex('/order', '/orderRecord')[0]
  let childPath = getPathIndex('/order', '/orderRecord')[1]
  eventBus.emit('activeParent', { path: '/order', index: parentPath })
  eventBus.emit('activeChild', childPath)
  router.push({ path: '/order/orderRecord', query: { uid: row.uid } })

}
const checkEvaluate = (row) => {
  let parentPath = getPathIndex('/order', '/evaluate')[0]
  let childPath = getPathIndex('/order', '/evaluate')[1]
  eventBus.emit('activeParent', { path: '/order', index: parentPath })
  eventBus.emit('activeChild', childPath)
  router.push({ path: '/order/evaluate', query: { uid: row.uid } })
}
</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
  // padding: 5px 0px;
}

:deep(.el-input__wrapper) {
  box-shadow: none;
}

:deep(.el-table .el-table__cell) {
  z-index: auto !important;
}
</style>
<style></style>
 